package com.hc.baselibray;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.example.baselibrary.ProcessConnection;
import com.example.baselibrary.R;

public class MessageService extends Service {

    static String TAG="MessageService";
    private  final  int MessageId=30;
//    @Override
//    public void onCreate() {
//        super.onCreate();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true){
//                    try {
//                        Thread.sleep(2000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    Log.d(TAG, "我偷偷在东北玩泥巴 ");
//                }
//
//            }
//        }).start();
//    }

    public MessageService() {
    }
    /**
     * 创建通知通道
     * @param channelId
     * @param channelName
     * @return
     */
    private String createNotificationChannel(String channelId, String channelName){
        NotificationChannel chan = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            chan = new NotificationChannel(channelId,
                    channelName, NotificationManager.IMPORTANCE_NONE);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            chan.setLightColor(Color.BLUE);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        }
        NotificationManager service = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            service.createNotificationChannel(chan);
        }
        return channelId;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: ");
         String CHANNEL_ID = "11";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CHANNEL_ID = createNotificationChannel("kim.hsl001", "ForegroundService002");
        } else {
            CHANNEL_ID = "";
        }
       // 通知的方式，提高进程的优先级
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Example Service")
                .setContentText("hello word")
                .setSmallIcon(R.mipmap.ib_partner_focus)
                .build();
        startForeground(MessageId,notification);
        //重点2
      //  bindService(new Intent(MessageService.this,GuardService.class),mServiceConnection,Service.BIND_IMPORTANT);

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        //重点1
        return  new ProcessConnection.Stub() {};
    }


    private ServiceConnection mServiceConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //连接成功
            Log.d(TAG, ": onServiceConnected连接成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            //断开,从新启动自己，重写绑定
            startService(new Intent(MessageService.this,GuardService.class));
            //重新绑定
            bindService(new Intent(MessageService.this,GuardService.class),mServiceConnection,Service.BIND_IMPORTANT);

        }
    };
}